/*
 * 键盘
 * 舞者无罪
 * 2014.12.01
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "lang.h"
#include "resource.h"
#include "func.h"
#include "com.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "mhedit.h"
#include "keypane_mac.h"
#include "mhedit.h"
#include "mhctrlstate.h"
#include "mac.h"


/*
 *
 */
static void CreateKeypaneMacInterface(HWND hWnd)
{
    int i = 0, j = 0;
    for(i = 0; i < 2; ++i)
    {
        for(j = 0; j < 4; ++j)
        {
            CreateWindow(MH_STATIC_NAME,
                "",
                WS_CHILD | WS_VISIBLE,
                ID_DAT_1NUM + i * 4 + j,
                3 + (CX_DAT_0BMP + 3) * j,
                3 + (CY_DAT_0BMP + 3) * i,
                CX_DAT_0BMP,
                CY_DAT_0BMP,
                hWnd, (int)&g_bmp[BMP_1DAT + i * 4 + j]);
        }
    }
    for(j = 0; j < 2; ++j)
    {
        CreateWindow(MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_DAT_1NUM + 8 + j,
        3 + (CX_DAT_0BMP + 3) * j,
        3 + (CY_DAT_0BMP + 3) * 2,
        CX_DAT_0BMP,
        CY_DAT_0BMP,
        hWnd, (int)&g_bmp[BMP_1DAT + 8 + j]);
    }
    CreateWindow(MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ENTER,
        3 + (CX_DAT_0BMP + 3) * 2,
        3 + (CY_DAT_0BMP + 3) * 2,
        CX_DAT_AUTO_MAP,
        CY_DAT_AUTO_MAP,
        hWnd, (int)&g_bmp[BMP_DAT_AUTO]);
    CreateWindow(MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        3,
        3 + (CY_DAT_0BMP + 3) * 3,
        CX_DAT_ESC1_BMP,
        CY_DAT_ESC1_BMP,
        hWnd, (int)&g_bmp[BMP_DAT_ESC1]);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    HWND hParent = GetHosting(hWnd);
    switch(nID)
    {
    case ID_DAT_1NUM + 9:
        {
            KeypaneOnKeyDown(hParent, SCANCODE_0);
        }
        break;
    case ID_DAT_1NUM:
        {
            KeypaneOnKeyDown(hParent, SCANCODE_1);
        }
        break;
    case ID_DAT_1NUM + 1:
        {
            KeypaneOnKeyDown(hParent, SCANCODE_2);
        }
        break;
    case ID_DAT_1NUM + 2:
        {
            KeypaneOnKeyDown(hParent, SCANCODE_3);
        }
        break;
    case ID_DAT_1NUM + 3:
        {
            KeypaneOnKeyDown(hParent, SCANCODE_4);
        }
        break;
    case ID_DAT_1NUM + 4:
        {
            KeypaneOnKeyDown(hParent, SCANCODE_5);
        }
        break;
    case ID_DAT_1NUM + 5:
        {
            KeypaneOnKeyDown(hParent, SCANCODE_6);
        }
        break;
    case ID_DAT_1NUM + 6:
        {
            KeypaneOnKeyDown(hParent, SCANCODE_7);
        }
        break;
    case ID_DAT_1NUM + 7:
        {
            KeypaneOnKeyDown(hParent, SCANCODE_8);
        }
        break;
    case ID_DAT_1NUM + 8:
        {
            KeypaneOnKeyDown(hParent, SCANCODE_9);
        }
        break;
    case ID_ESC:
        {
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    case ID_ENTER:
        {
            HWND hTwd = GetDlgItem(hParent, ID_CYCLE_GROUP);
            SetCtrlState(hTwd, !GetCtrlState(hTwd));
            InvalidateRect(hTwd, NULL, TRUE);
            MacCycleButton(hParent, ID_CYCLE_GROUP);
         }
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, ID_ENTER);
    }
    else if(key == SCANCODE_ESCAPE)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_PERIOD)
    {
        OnBtnCmd(hWnd, ID_ENTER);
    }
    else if(key == SCANCODE_0)
    {
        OnBtnCmd(hWnd, ID_DAT_1NUM + 9);
    }
    else if(key == SCANCODE_9)
    {
       OnBtnCmd(hWnd, ID_DAT_1NUM + 8);
    }
    else if(key == SCANCODE_1)
    {
        OnBtnCmd(hWnd, ID_DAT_1NUM);
    }
    else if(key == SCANCODE_2)
    {
       OnBtnCmd(hWnd, ID_DAT_1NUM + 1);
    }
    else if(key == SCANCODE_3)
    {
        OnBtnCmd(hWnd, ID_DAT_1NUM + 2);
    }
    else if(key == SCANCODE_4)
    {
        OnBtnCmd(hWnd, ID_DAT_1NUM + 3);
    }
    else if(key == SCANCODE_5)
    {
       OnBtnCmd(hWnd, ID_DAT_1NUM + 4);
    }
    else if(key == SCANCODE_6)
    {
        OnBtnCmd(hWnd, ID_DAT_1NUM + 5);
    }
    else if(key == SCANCODE_7)
    {
        OnBtnCmd(hWnd, ID_DAT_1NUM + 6);
    }
    else if(key == SCANCODE_8)
    {
        OnBtnCmd(hWnd, ID_DAT_1NUM + 7);
    }
}
 /*
 *
 */
static int KeypaneMacWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
            CreateKeypaneMacInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_KEYPANE_MAC]);
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateKeypaneMacWindow(HWND hHosting)
{
    HWND hWnd;
    MAINWINCREATE CreateInfo;

    int cx = 300;
    int cy = 200;
    WND_EXTRA_DATA data;
    CreateInfo.dwStyle = WS_VISIBLE ;//| WS_CHILD;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = "";
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = KeypaneMacWinProc;
    CreateInfo.lx = cx;
    CreateInfo.ty = cy;
    CreateInfo.rx = cx + CX_KEYPANE_MAC_BMP;
    CreateInfo.by = cy + CY_KEYPANE_MAC_BMP;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = data.value;
    CreateInfo.hHosting = hHosting;
    hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, ID_DAT_1NUM));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
